package com.loan.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.loan.entity.Emp;
import com.loan.util.AUtils;
import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

public interface EmpMapper extends BaseMapper<Emp> {

    @Insert("insert into emp(account,password,status,name,gender,role_id) values(#{account},#{password},#{status},#{name},#{gender},#{roleid})")
    void addEmp(Emp emp);

    @Update("UPDATE emp SET status='n' where id=#{id}")
    void deleteStatus(int id);

    @Select("select * from emp where status='y'")
    List<Emp> getAll();

    @Select("select * from emp where account=#{account}")
    Emp getEmpAccount(String account);

    @UpdateProvider(type = SqlProvider.class,method = "update")
    Void updateEmp(Emp emp);

    @Select("select account,name from emp where id=#{id}")
    public Emp getEmpInfo(Integer id);

    public class SqlProvider{
        public String update(Emp emp){
            SQL sql = new SQL();
            sql.UPDATE("emp");
            if(AUtils.isNotEmpty(emp.getAccount())){
                sql.SET("account=#{account}");
            }
            if(AUtils.isNotEmpty(emp.getPassword())){
                sql.SET("password=#{password}");
            }
            if(AUtils.isNotEmpty(emp.getStatus())){
                sql.SET("status=#{status}");
            }
            if(AUtils.isNotEmpty(emp.getGender())){
                sql.SET("gender=#{gender}");
            }
            if(AUtils.isNotEmpty(emp.getName())){
                sql.SET("name=#{name}");
            }
            if(emp.getRoleid() != 0){
                sql.SET("role_id=#{roleid}");
            }
            sql.WHERE("id = #{id}");

            return sql.toString();
        }
    }
}